﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data.OleDb;
using System.Data;
using Mvc4Access.Models.Utility;
using Mvc4Access.Models.BusinessEntity;

namespace Mvc4Access.Models.DataAccessLib
{
    public class sysMenuDal
    {
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid menuID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from sysMenu");
            strSql.Append(" where menuID=@menuID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@menuID", OleDbType.VarChar,40)};
            parameters[0].Value = menuID;

            return DbHelperOleDb.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(sysMenu model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into sysMenu(");
            strSql.Append("menuID,menuCode,menuParentCode,menuUrl,createDate,lineNo,menuName)");
            strSql.Append(" values (");
            strSql.Append("@menuID,@menuCode,@menuParentCode,@menuUrl,@createDate,@lineNo,@menuName)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@menuID", OleDbType.VarChar,40),
					new OleDbParameter("@menuCode", OleDbType.VarChar,255),
					new OleDbParameter("@menuParentCode", OleDbType.VarChar,255),
					new OleDbParameter("@menuUrl", OleDbType.VarChar,255),
					new OleDbParameter("@createDate", OleDbType.Date),
					new OleDbParameter("@lineNo", OleDbType.Integer,4),
					new OleDbParameter("@menuName", OleDbType.VarChar,255)};
            parameters[0].Value = Guid.NewGuid();
            parameters[1].Value = model.menuCode;
            parameters[2].Value = model.menuParentCode;
            parameters[3].Value = model.menuUrl;
            parameters[4].Value = model.createDate;
            parameters[5].Value = model.lineNo;
            parameters[6].Value = model.menuName;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(sysMenu model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update sysMenu set ");
            strSql.Append("menuCode=@menuCode,");
            strSql.Append("menuParentCode=@menuParentCode,");
            strSql.Append("menuUrl=@menuUrl,");
            strSql.Append("createDate=@createDate,");
            strSql.Append("lineNo=@lineNo,");
            strSql.Append("menuName=@menuName");
            strSql.Append(" where menuID=@menuID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@menuCode", OleDbType.VarChar,255),
					new OleDbParameter("@menuParentCode", OleDbType.VarChar,255),
					new OleDbParameter("@menuUrl", OleDbType.VarChar,255),
					new OleDbParameter("@createDate", OleDbType.Date),
					new OleDbParameter("@lineNo", OleDbType.Integer,4),
					new OleDbParameter("@menuName", OleDbType.VarChar,255),
					new OleDbParameter("@menuID", OleDbType.VarChar,40)};
            parameters[0].Value = model.menuCode;
            parameters[1].Value = model.menuParentCode;
            parameters[2].Value = model.menuUrl;
            parameters[3].Value = model.createDate;
            parameters[4].Value = model.lineNo;
            parameters[5].Value = model.menuName;
            parameters[6].Value = model.menuID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid menuID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sysMenu ");
            strSql.Append(" where menuID=@menuID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@menuID", OleDbType.VarChar,40)};
            parameters[0].Value = menuID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string menuIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sysMenu ");
            strSql.Append(" where menuID in (" + menuIDlist + ")  ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public sysMenu GetModel(Guid menuID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select menuID,menuCode,menuParentCode,menuUrl,createDate,lineNo,menuName from sysMenu ");
            strSql.Append(" where menuID=@menuID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@menuID", OleDbType.VarChar,40)};
            parameters[0].Value = menuID;

            sysMenu model = new sysMenu();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["menuID"] != null && ds.Tables[0].Rows[0]["menuID"].ToString() != "")
                {
                    model.menuID = new Guid(ds.Tables[0].Rows[0]["menuID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["menuCode"] != null && ds.Tables[0].Rows[0]["menuCode"].ToString() != "")
                {
                    model.menuCode = ds.Tables[0].Rows[0]["menuCode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["menuParentCode"] != null && ds.Tables[0].Rows[0]["menuParentCode"].ToString() != "")
                {
                    model.menuParentCode = ds.Tables[0].Rows[0]["menuParentCode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["menuUrl"] != null && ds.Tables[0].Rows[0]["menuUrl"].ToString() != "")
                {
                    model.menuUrl = ds.Tables[0].Rows[0]["menuUrl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["createDate"] != null && ds.Tables[0].Rows[0]["createDate"].ToString() != "")
                {
                    model.createDate = DateTime.Parse(ds.Tables[0].Rows[0]["createDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["lineNo"] != null && ds.Tables[0].Rows[0]["lineNo"].ToString() != "")
                {
                    model.lineNo = int.Parse(ds.Tables[0].Rows[0]["lineNo"].ToString());
                }
                if (ds.Tables[0].Rows[0]["menuName"] != null && ds.Tables[0].Rows[0]["menuName"].ToString() != "")
                {
                    model.menuName = ds.Tables[0].Rows[0]["menuName"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select menuID,menuCode,menuParentCode,menuUrl,createDate,lineNo,menuName ");
            strSql.Append(" FROM sysMenu ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OleDbParameter[] parameters = {
                    new OleDbParameter("@tblName", OleDbType.VarChar, 255),
                    new OleDbParameter("@fldName", OleDbType.VarChar, 255),
                    new OleDbParameter("@PageSize", OleDbType.Integer),
                    new OleDbParameter("@PageIndex", OleDbType.Integer),
                    new OleDbParameter("@IsReCount", OleDbType.Boolean),
                    new OleDbParameter("@OrderType", OleDbType.Boolean),
                    new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
                    };
            parameters[0].Value = "sysMenu";
            parameters[1].Value = "menuID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        

        #endregion  Method
    }
}